package com.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;
import com.gulimall.product.dao.SkuSaleAttrValueDao;
import com.gulimall.product.entity.SkuSaleAttrValueEntity;
import com.gulimall.product.service.SkuSaleAttrValueService;
import com.gulimall.product.vo.SkuItemVO;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuSaleAttrValueService")
public class SkuSaleAttrValueServiceImpl extends ServiceImpl<SkuSaleAttrValueDao, SkuSaleAttrValueEntity> implements SkuSaleAttrValueService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuSaleAttrValueEntity> page = this.page(new Query<SkuSaleAttrValueEntity>().getPage(params),
                new QueryWrapper<SkuSaleAttrValueEntity>());

        return new PageUtils(page);
    }

    @Override
    public void saveBatchSaleAttr(List<SkuSaleAttrValueEntity> valueEntities) {
        saveBatch(valueEntities);
    }

    @Override
    public List<SkuItemVO.SkuItemSaleAttrVo> getSkuSaleAttrs(Long spuId) {
        return baseMapper.getSkuSaleAttrs(spuId);
    }

    @Override
    public List<String> getSkuAttr(Long skuId) {
        List<SkuSaleAttrValueEntity> valueEntities = list(new QueryWrapper<SkuSaleAttrValueEntity>().eq("sku_id",
                skuId));

        return valueEntities.stream().map(item -> {
            return String.format("%s:%s", item.getAttrName(), item.getAttrValue());
        }).collect(Collectors.toList());
    }

}
